package com.sghj.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;

public class smallMethods {
    //取出日期的方法
    public String getDate(Date longDate) throws ParseException {
        String[] strNow3 = new SimpleDateFormat("yyyy-MM-dd").format(longDate).toString().split("-");
        String stime=strNow3[0]+"-"+strNow3[1]+"-"+strNow3[2];
        return stime;
    }
    public String getDate3(Date longDate) throws ParseException {
        String[] strNow3 = new SimpleDateFormat("yyyy-MM").format(longDate).toString().split("-");
        String stime=strNow3[0]+"-"+strNow3[1];
        return stime;
    }
    public String getDate2(Date longDate) throws ParseException {
        String[] strNow3 = new SimpleDateFormat("HH:mm:ss").format(longDate).toString().split(":");
        String stime=strNow3[0]+":"+strNow3[1]+":"+strNow3[2];
        return stime;
    }
    //日期拼接时刻
    public Date getWishedTime(String date,String time) throws ParseException {
        String fullTime=date+" "+time;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date wishedT = simpleDateFormat.parse(fullTime);
        return wishedT;
    }
    //第二天
    public Date nextDay(Date today){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(today);
        calendar.add(Calendar.DATE, 1);
        Date nextDay=calendar.getTime();
        return nextDay;
    }
    //Date转String
    public String stringDate(Date date){
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String strDate1 = sdf1.format(date);
        return strDate1;
    }
    //Date转String
    public String stringDateDetail(Date date){
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate1 = sdf1.format(date);
        return strDate1;
    }
    //时间多加一分钟
    public Date nextMin(Date date){
        Date nextMin = new Date(date.getTime() + 60000);
        return nextMin;
    }
    //获取前一天
    public String beforeday(Date da) throws ParseException {
        Calendar c = Calendar.getInstance();

        c.setTime(da);
        c.add(Calendar.DAY_OF_MONTH, -1);

        Date yesterday = c.getTime();//这是昨天
        String yes= getDate(yesterday);
        return yes;
    }
    //Date类型转为ZonedDateTime
    public static ZonedDateTime toZonedDateTime(Date date){
        if(date==null){
            return null;
        }
        final ZoneId systemDefault= ZoneId.systemDefault();
        return ZonedDateTime.ofInstant(date.toInstant(),systemDefault);
    }
    //看两个时间是否有重叠
    /**
     * 判断2个时间段是否有重叠（交集）
     * @param startDate1 时间段1开始时间戳
     * @param endDate1 时间段1结束时间戳
     * @param startDate2 时间段2开始时间戳
     * @param endDate2 时间段2结束时间戳
     * @param isStrict 是否严格重叠，true 严格，没有任何相交或相等；false 不严格，可以首尾相等，比如2021/5/29-2021/5/31和2021/5/31-2021/6/1，不重叠。
     * @return 返回是否重叠
     */
    public boolean isOverlap(long startDate1, long endDate1, long startDate2, long endDate2, boolean isStrict){
        if(endDate1<startDate1){
            throw new DateTimeException("endDate1不能小于startDate1");
        }
        if(endDate2<startDate2){
            throw new DateTimeException("endDate2不能小于startDate2");
        }
        if(isStrict){
            if(! (endDate1<startDate2 || startDate1>endDate2)){
                System.out.println("有重叠");
                return true;
            }
        }else{
            if(! (endDate1<=startDate2 || startDate1>=endDate2)){
                return true;
            }
        }
        System.out.println("没有重叠");
        return false;
    }
    public boolean issameday(Date date1,Date date2){
        SimpleDateFormat fmt=new SimpleDateFormat("yyyyMMdd");
        return fmt.format(date1).equals(fmt.format(date2));
    }
    //返回两个日期之间的所有日期
    public static List<String> getBetweenDate(String startTime, String endTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime()<=endDate.getTime()){
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.DATE, 1);
                // 获取增加后的日期
                startDate=calendar.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }

    //返回两个日期之间的所有年月
    public  List<String> getBetweenMonth(String startTime, String endTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime()<=endDate.getTime()){
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.MONTH, 1);
                // 获取增加后的日期
                startDate=calendar.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }


    //string转Date
    public Date stoD(String time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Date date = simpleDateFormat.parse(time);
        return date;

    }
    public Date stoD3(String time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");

        Date date = simpleDateFormat.parse(time);
        return date;

    }
    public Date stoD2(String time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");

        Date date = simpleDateFormat.parse(time);
        return date;

    }


    public List<String> getBetweenDateNoLastDay(String start, String end) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(start);
            Date endDate = sdf.parse(end);

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime()<endDate.getTime()){
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.DATE, 1);
                // 获取增加后的日期
                startDate=calendar.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }
}
